package com.rivercloud.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;

import com.alibaba.fastjson.JSONObject;
import com.rivercloud.dao.ProjectDao;
import com.rivercloud.dao.TableDao;
import com.rivercloud.dao.UserDao;
import com.rivercloud.dao.UserTableDao;
import com.rivercloud.dao.Impl.ProjectDaoImpl;
import com.rivercloud.dao.Impl.TableDaoImpl;
import com.rivercloud.dao.Impl.UserDaoImpl;
import com.rivercloud.dao.Impl.UserTableDaoImpl;
import com.rivercloud.entity.ExcelTable;
import com.rivercloud.entity.Project;
import com.rivercloud.entity.User;
import com.rivercloud.entity.UserTable;

/**
 * Created by lq on 2016/11/29/0029.
 */
@At("/user")
@Fail("http:500")
public class UserController {

	@Inject
	private UserDao userDao = new UserDaoImpl();
	private UserTableDao userTableDao = new UserTableDaoImpl();
	private TableDao tableDao = new TableDaoImpl();
	private ProjectDao projectDao = new ProjectDaoImpl();
	

	@At("/regist")
	@Ok("jsp:jsp.login")
	public void userRegist(HttpServletRequest request) {
		try {
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			String name = request.getParameter("name");
			String email = request.getParameter("email");
			User user = new User();
			user.setUserName(userName);
			user.setPassword(password);
			user.setName(name);
			user.setEmail(email);
			userDao.addUser(user);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@At("/login")
	@Filters
	public void userLogin(HttpServletRequest request, HttpServletResponse response) {
		try {
			// zdb_18.3.5_start
			String prePage = (String) request.getSession().getAttribute("prePage");
			System.out.println(prePage);

			if (prePage == null) {

				String userName = request.getParameter("userName");
				String password = request.getParameter("password");
				User user = new User();
				user.setUserName(userName);
				user.setPassword(password);
				User existUser = userDao.userLogin(user);
				System.out.println("user=" + existUser);
				if (existUser != null) {

					request.getSession().setAttribute("user", existUser);
					response.getWriter().print(2);
				} else {
					System.out.println("不正确");
					response.getWriter().print(1);

				}

			} else {
				/*String path = "http://180.101.144.29:8085" + prePage;*/
				String path ="http://" + request.getServerName() + ":" + request.getServerPort() + prePage;
				
				String userName = request.getParameter("userName");
				String password = request.getParameter("password");
				User user = new User();
				user.setUserName(userName);
				user.setPassword(password);
				User existUser = userDao.userLogin(user);
				System.out.println("user=" + existUser);
				if (existUser != null) {
					String temp = prePage.substring(prePage.length()-5, prePage.length());
					String[] str = temp.split("=");
					
					int projectId = Integer.parseInt(str[1]);
					
					Project project = projectDao.findProjectById(projectId);
					

					request.getSession().setAttribute("user", existUser);
					request.getSession().setAttribute("project", project);
					JSONObject jObject=new JSONObject();
				    jObject.put("path", path);
				    response.getWriter().print(jObject);
				} else {
					System.out.println("不正确");
					response.getWriter().print(1);

				}
				

			}

			

			// zdb_18.3.5_end

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	
	@At("/checkLogin")
	@Ok("json")
	 @Filters
	public Map checkLogin(HttpServletRequest request,HttpServletResponse response) {
		try {
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			User user = new User();
			user.setUserName(userName);
			user.setPassword(password);
			User existUser = userDao.userLogin(user);
			System.out.println("user=" + existUser);
			Map map = new HashMap<>();
			if (existUser == null) {
			System.out.println("1---");
			response.getWriter().print(1);	
			map.put("true", true);
				return map;
			}
			
			
			

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	@At("/loginOut")
	@Ok("jsp:jsp.login")
	@Filters
	public void loginOut(HttpServletRequest request) {
		try {
			
			request.getSession().invalidate();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
	}

	@At("/checkUserName")
	public void checkUserName(HttpServletRequest request, HttpServletResponse response) {
		try {
			String userName = request.getParameter("userName");
			User existUser = userDao.checkUserName(userName);
			if (existUser == null) {
				response.getWriter().print("1");
			} else {
				response.getWriter().print("2");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	@At("/checkPassword")
	public void checkPassword(HttpServletRequest request, HttpServletResponse response) {
		try {
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			User existUser = userDao.checkUserName(userName);
			String existPassword = existUser.getPassword();
			if (existPassword.equals(password)) {
				response.getWriter().print("1");
			} else {
				response.getWriter().print("2");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@At("/collection")
	
	public void collection(HttpServletRequest request) {
		try {
			int userId =  Integer.parseInt(request.getParameter("userId"));
			int tableId = Integer.parseInt(request.getParameter("tableId"));
			int projectId = tableDao.findProjectIdByTableId(tableId);
			ExcelTable table = tableDao.findTableById(tableId);
			
			Project project = projectDao.findProjectById(projectId);
			
			UserTable userTable = new UserTable();
			userTable.setTableId(tableId);
			userTable.setUserId(userId);
			userTable.setCreator(table.getCreator());
			userTable.setTableName(table.getTableName());
			userTable.setProjectName(project.getProjectName());
			
			userTableDao.addUserTable(userTable);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	@At("/deleteCollection")
	
	public void deleteCollection(HttpServletRequest request) {
		try {
			int userId =  Integer.parseInt(request.getParameter("userId"));
			int tableId = Integer.parseInt(request.getParameter("tableId"));
			
			UserTable userTable = new UserTable();
			userTable.setTableId(tableId);
			userTable.setUserId(userId);
			
			userTableDao.deleteUserTable(userTable);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	
	public UserTable checkCollection(int userId,int tableId) {
		
			UserTable userTable = new UserTable();
			userTable.setTableId(tableId);
			userTable.setUserId(userId);
			
			UserTable existUserTable = userTableDao.checkCollection(userTable);
			return existUserTable;
			
		
	}
	
	@At("/userInfo")
	@Ok("jsp:jsp.collectionTable")
	public void userInfo(HttpServletRequest request) {
		try {
			int userId = Integer.parseInt(request.getParameter("userId"));
			User user = userDao.findUserById(userId);
			List<UserTable> userTableList = userTableDao.findUserTableByUserId(userId);
			
			request.setAttribute("user", user);
			request.setAttribute("userTableList", userTableList);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	

	}
	
	@At("/changePassword")
	@Ok("json")
	public Map changePassword(HttpServletRequest request) {
		
			String  userName = request.getParameter("userName");
			String  password = request.getParameter("newPassword");
			User user = new User();
			user.setUserName(userName);
			user.setPassword(password);
			Map map = new HashMap ();
			userDao.changePassword(user);
			map.put("true", true);
			return map;
		
		
		
	}

}
